<?php
/**
 * Created by Mr.hu.
 * Date: 2017/3/3
 * Time: 10:52
 * Description:'描述'
 */
namespace bc\model\coupons;
use bc\ar\MActiveRecord;

class CouponsUserModel extends MActiveRecord{

    //错误信息
    public $_error_message;
    private $user_ids_arr;
    private $coupons_id;
    private $number;

    public static $bc_coupons_user_status=[
        '0'=>'未使用',
        '1'=>'已使用',
        '2'=>'赠送中',
        '3'=>'已赠送',
        '4'=>'已过期'
    ];

    public static function tableName()
    {
        return "{{%coupons_user}}";
    }


    /**
     * Created by Mr.hu.
     * @param $data
     * Description:验证数据，返回验证后的数据
     */
    public function self_validate($data)
    {
        //优惠券的数量
        $number=intval($data['number']);
        if($number===0){
            $this->_error_message='优惠券的数量不能为0';
            return false;
        }
        $this->number=$number;

        //对应的用户ids
        $user_ids=trim($data['user_ids']);
        if(empty($user_ids)){
            $this->_error_message='请选择用户';
            return  false;
        }

        $coupons_id=intval($data['coupons_id']);
        if($coupons_id===0){
            $this->_error_message='无效的优惠券';
            return false;
        }else{
            $try_find=$this->find()->where(array('coupons_id'=>$coupons_id))->from(CouponsModel::tableName())->one();
            if(!$try_find){
                $this->_error_message='优惠券不存在';
                return false;
            }
        }
        $this->coupons_id=$coupons_id;
        //数组
        $after_ids=explode(',',$user_ids);
        $this->user_ids_arr=$after_ids;
        return  true;
    }


    /**
     * Created by Mr.hu.
     * Description:生产优惠券
     */
    public function make_coupons()
    {
        $now_time=date('Y-m-d H:i:s');
        $activeModel=\Yii::$app->db;
        $insert_fields=[
            'user_id'       ,
            'from_user_id'  ,
            'coupons_id'    ,
            'status'        ,
            'create_time'   ,
            'update_time'   ,
            'is_del'        ,
        ];
        $transaction =$activeModel->beginTransaction();
        try{
            foreach ($this->user_ids_arr as $item){
                $temp_arr=array($item,0,$this->coupons_id,0,$now_time,$now_time,0);
                $insert_data=array_fill(0,$this->number,$temp_arr);
                $activeModel->createCommand()->batchInsert($this->tableName(),$insert_fields,$insert_data)->execute();
            }
            $transaction ->commit();
            return true;
        }catch (\Exception $e){
            $transaction->rollBack();
            return false;
        }

    }


    /**
     * Created by Mr.hu.
     * @param $coupons_id 优惠券的id
     * @param $page 分页
     * @param $page_size 每页大小
     * Description:
     */
    public function getCouponsListById($coupons_id,$page,$page_size)
    {
        $list=$this->find()
            ->where(array('coupons_id'=>$coupons_id))
            ->offset(($page-1)*$page_size)
            ->limit($page_size)
            ->asArray()
            ->all();
        return $list;
    }

	
    /**
     * 
     */
    public function getFrontendRow($params)
    {
    	$ar = $this->find()->select($this->columns)->leftJoin(CouponsModel::getTableSchema()->name, CouponsModel::getTableSchema()->name. '.coupons_id = ' . self::getTableSchema()->name . '.coupons_id');
    	$ar->where($params);
    	return $ar->one();
    }
    
    
    /**
     * 获取需要使用的优惠券
     */
    public function getUseCoupon($params)
    {
    	$ar = $this->find()->select($this->columns)->leftJoin(CouponsModel::getTableSchema()->name, CouponsModel::getTableSchema()->name. '.coupons_id = ' . self::getTableSchema()->name . '.coupons_id');
    	$ar->where($params);
    	$ar->orderBy(self::getTableSchema()->name . '.from_user_id DESC');
    	$list = $ar->asArray()->all();
    	return $list[0];
    }



}